/*****************************************************************************
* File Name: Interrupts.c
* Version 
*
* Description: 
* Description: This file contains interrupt service routines for all the interrupts
*               in the system
*
* Owner:
* SIRK
*    
* Related Document:
*     
*
* Hardware Dependency:
* CYPRESS BLE Dongle
*
* Code Tested With:
* PSoC Creator    
*
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#include "USB_Interface.h"

/*******************************************************************************
* Function Name: InDMADone_Interrupt
********************************************************************************
* Summary:
*   The Interrupt Service Routine for isr_InDMADone. Arms USB Audio In end-point
*     for USB host to receive audio data.
*
* Parameters:  
*    InDMADone interrupt vector
*
* Return:
*   None
*
* Theory:
*  USB EP will be loaded with data picked from the Circular buffer using InDMA
*
* Side Effects:
* Updates the circular buffer level.
*
*******************************************************************************/
CY_ISR(InDMADone_Interrupt)
{
    /* Parameters to this API Call CHANGES DEPENDING ON DATA ENDPOINT MEMORY MODE */
    USBFS_LoadInEP(AUDIO_IN_ENDPOINT, USBFS_NULL, AUDIO_SAMPLE_SIZE);

    /* Update of inUsbCount needs to be atomic */
    isr_RxDMADone_Disable();
    inLevel -= AUDIO_SAMPLE_SIZE;
    
    isr_RxDMADone_Enable();

    HandleUnderflow();
}

/*******************************************************************************
* Function Name: RxDMADone_Interrupt
********************************************************************************
* Summary:
*   The Interrupt Service Routine for isr_RxDMADone. This handles the AUDIO In
*    buffer pointers and detects overflow of the buffer to stop AUDIO In. 
*
* Parameters:  
*    RxDMADone interrupt vector
*
* Return:
*   void.
*
* Theory:
*  Whenever the data from Transmitter is recieved the buffer level will be increased by input transaction size
*
* Side Effects:
* Updates the circular buffer level.
* Updates the RXDMA Done flag, to inform that DMA is ready for trigger.
*
*******************************************************************************/
CY_ISR(RxDMADone_Interrupt)
{
    isr_InDMADone_Disable();

    /*If sync packet occurs set the inlevel to match the TX device buffer size*/
#ifdef RESET_BUF_LEVEL
    if(0 != txDataDropSize)
    {
        inLevel = txDataDropSize;
        txDataDropSize = 0;
    }
    else
#endif /*RESET_BUF_LEVEL*/
    {
        inLevel += IN_TRANS_SIZE;
    }
    isr_InDMADone_Enable();
        
    RxDMADone = true;
}

/*******************************************************************************
* Function Name: isr_RemoteWakeup
********************************************************************************
*
* Summary:
*  ISR Callback function for remote wakeup pin.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  Device will wakeup when the trigger is set by BLE device and USB Host will be set to wakeup
*
* Side Effects:
*
* Note:
*
*******************************************************************************/
CY_ISR(isr_RemoteWakeup)
{
    Pin_RemoteWakeup_ClearInterrupt();
}

/*******************************************************************************
* Function Name: isr_PktDecoder
********************************************************************************
*
* Summary:
*  Handles the Interrupt Service Routine for the UART Timer.
*  Resets RX command state m/c for every 100ms delay on RX byte
*  If RX packet has started, there should not be more than 100ms delay between 
*  each RX byte with-in the packet.
*
* Parameters:
*  void
*
* Return:
*  void
*
* Theory:
*  None
*
* Side Effects:
*  State m/c and other parameters will be reset
*
* Note:
*  This ISR can cause packet to be dropped by the decoder if there exist delay for bytes with-in the packet.
*******************************************************************************/
CY_ISR(isr_PktDecoder)
{
    uint32 intrSrc = Timer_PktDecoder_ReadStatusRegister();
   
    if (Timer_PktDecoder_STATUS_TC & intrSrc)
    {
        /*Reset RX state m/c*/
        pktRxState = STATE_PACKET_HEADER_CODE;
        /*Reset receive counter*/
        curPacketIndex = 0;
#ifdef DEBUG_W_LED
        Pin_StatusLED_Write(Pin_StatusLED_Read() ^ 0x01);
#endif /*DEBUG_W_LED*/
    }
    else
    {
        /*Ignore other interrupt sources*/
    }

    isr_PktDecoder_ClearPending();
}

/* [] END OF FILE */
